package com.boot.study;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.ObjectId;
import com.boot.study.domain.FileTest;
import org.dromara.pdf.fop.core.doc.Document;
import org.dromara.pdf.fop.core.doc.component.block.BlockContainer;
import org.dromara.pdf.fop.core.doc.component.image.Image;
import org.dromara.pdf.fop.core.doc.component.line.SplitLine;
import org.dromara.pdf.fop.core.doc.component.table.Table;
import org.dromara.pdf.fop.core.doc.component.table.TableBody;
import org.dromara.pdf.fop.core.doc.component.table.TableCell;
import org.dromara.pdf.fop.core.doc.component.table.TableRow;
import org.dromara.pdf.fop.core.doc.component.text.Text;
import org.dromara.pdf.fop.core.doc.page.Page;
import org.dromara.pdf.fop.handler.TemplateHandler;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Main {


    /**
     * 测试表格显示方式 单双行
     */

    public void testFileShow() {
        Document document = TemplateHandler.Document.build();
        // 创建页面
        Page page = TemplateHandler.Page.build();
        page.setMarginTop("20pt")
                .setFontFamily("宋体")
                .setBodyMarginTop("30pt")
                .setMarginBottom("10pt")
                .setWidth("600pt")
                .setHeight("700pt");
        page.setBodyBackgroundColor("#CCE8CF");
        this.initHeader(page);
        this.initFooter(page);

        List<FileTest> fileTests = this.initTableHeader();
        List<FileTest> testList = this.initTableData();
        Table table = TemplateHandler.Table.build();
        TableBody tableBody = TemplateHandler.Table.Body.build();


        table.setBody(tableBody);
        page.addBodyComponent(table);
        document.addPage(page);
        document.transform("D:\\pdf\\test\\fo\\" + ObjectId.next() + ".pdf");
    }


    private List<FileTest> initTableHeader() {
        List<FileTest> data = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            FileTest fileTest = new FileTest();
            fileTest.setKey("字段" + i);
            if (i % 2 == 0) {
                fileTest.setShowType("0");
            } else {
                fileTest.setShowType("1");
            }
            data.add(fileTest);
        }


        return data;
    }

    private List<FileTest> initTableData() {
        List<FileTest> data = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            FileTest fileTest = new FileTest();
            fileTest.setKey("参数" + i);
            if (i % 2 == 0) {
                fileTest.setShowType("0");
            } else {
                fileTest.setShowType("1");
            }
            data.add(fileTest);
        }


        return data;
    }

    /**
     * 测试表格显示形式
     */
    @Test
    public void testRow() {
        Document document = TemplateHandler.Document.build();
        // 创建页面
        Page page = TemplateHandler.Page.build();
        page.setMarginTop("20pt")
                .setFontFamily("宋体")
                .setBodyMarginTop("30pt")
                .setMarginBottom("10pt")
                .setWidth("600pt")
                .setHeight("700pt");
        page.setBodyBackgroundColor("#CCE8CF");
//        this.initHeader(page);
//        this.initFooter(page);
        Table table = TemplateHandler.Table.build();
        TableBody tableBody = TemplateHandler.Table.Body.build();
        TableRow tableRow = TemplateHandler.Table.Row.build();
        tableRow.setMinRowHeight("20pt");
        tableRow.addCell(TemplateHandler.Table.Cell.build().setWidth("50pt")
                .addComponent(TemplateHandler.Text.build().setText("序号")));
        tableRow.addCell(TemplateHandler.Table.Cell.build().setWidth("150pt")
                .addComponent(TemplateHandler.Text.build().setText("名称")));
        TableRow tableRow2 = TemplateHandler.Table.Row.build();
        for (int i = 1; i <= 4; i++) {
            tableRow2.addCell(TemplateHandler.Table.Cell.build().setWidth("50pt")
                    .addComponent(TemplateHandler.Text.build().setText("序号" + i)));
        }

        tableBody.addRow(tableRow2);

        TableRow tableRow3 = TemplateHandler.Table.Row.build();
        for (int i = 1; i <= 3; i++) {
            tableRow3.addCell(TemplateHandler.Table.Cell.build().setWidth("50pt")
                    .addComponent(TemplateHandler.Text.build().setText("姓名：" + i)));
        }

        TableRow tableRow4 = TemplateHandler.Table.Row.build();
        tableRow4.addCell(TemplateHandler.Table.Cell.build().setWidth("50pt")
                .addComponent(TemplateHandler.Text.build().setText("姓名：888")));


        tableBody.addRow(tableRow4);


        tableBody.addRow(tableRow);
        for (int i = 0; i < 10; i++) {
            TableRow row = TemplateHandler.Table.Row.build();
            row.addCell(TemplateHandler.Table.Cell.build().setWidth("50pt")
                    .addComponent(TemplateHandler.Text.build().setText("姓名：888")));
            tableBody.addRow(row);
        }
        table.setBody(tableBody);


        page.addBodyComponent(table);
        document.addPage(page);
        document.transform("D:\\pdf\\test\\fo\\" + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".pdf");


    }


    @Test
    public void testTitleText() {
        Document document = TemplateHandler.Document.build();
        // 创建页面
        Page page = TemplateHandler.Page.build();
        page.setMarginTop("20pt")
                .setFontFamily("宋体")
                .setBodyMarginTop("30pt")
                .setMarginBottom("10pt")
                .setWidth("600pt")
                .setHeight("700pt");
        page.setBodyBackgroundColor("#CCE8CF");
        this.initHeader(page);
        this.initFooter(page);
        Text title = TemplateHandler.Text.build().setText("hello world")
                .setHorizontalStyle("center").setFontFamily("宋体")
                .setFontWeight("bolder").setFontSize("30pt");
        page.addBodyComponent(title);

        for (int i = 1; i <= 50; i++) {
            Table table = TemplateHandler.Table.build().enableKeepWithPrevious();
            this.tableData(table);
            table.setWidth("500pt").setMarginLeft("50pt");
            page.addBodyComponent(table);
        }


        document.addPage(page);
        document.transform("D:\\pdf\\test\\fo\\" + ObjectId.next() + ".pdf");
        // 上传文件服务器
//        ByteArrayOutputStream bs = new ByteArrayOutputStream())
//            document.transform(bs);
//            String fileApiFile = fileApi.createFile(pdfName,HttpURL.PATH+pdfName, bs.toByteArray(), "ctms",
//                    proId, null, null, null,
//                    null, null, null, null,
//                    "2");
//            log.info("文件上传之后的路径fileApiFile:{}",fileApiFile);

    }


    @Test
    public void testJian() throws FileNotFoundException {
        Document document = TemplateHandler.Document.build();
        // 创建页面
        Page page = TemplateHandler.Page.build();
        page.setWidth("600px")
                .setMarginBottom("20pt")
                .setMarginLeft("55pt")
                .setMarginRight("55pt")
                .setBodyMarginTop("14pt")
                .setFontFamily("宋体");
        // 创建表单
        Table table = TemplateHandler.Table.build().setBorder("1 solid black")
                .setMinRowHeight("30px")
                .setWidth("490px");
        // 创建表格替
        TableBody tableBody = TemplateHandler.Table.Body.build();
        // 创建行
        TableRow tableRow = TemplateHandler.Table.Row.build().setBorder("1 solid black")
                .setMinRowHeight("20px").setHorizontalStyle("center");
        // 创建表名
        tableRow.addCell(TemplateHandler.Table.Cell.build().setFontFamily("宋体")
                .setHorizontalStyle("left").setBackgroundColor("#B4C6E7").setColumnSpan(2)
                .setFontSize("15pt").addComponent(TemplateHandler.Text.build().setText("测试表")));
        tableBody.addRow(tableRow);

        TableRow row = TemplateHandler.Table.Row.build()
                .setBorder("solid").setMinRowHeight("20").setHorizontalStyle("center").setBorderStyle("yellow")
                .enableKeepTogether();
        //  第一个表格数据

        row.addCell(TemplateHandler.Table.Cell.build().setFontSize("10").setWidth("345px")
                .setFontFamily("宋体").setHorizontalStyle("left").setBorder("solid")//.setBorderColor("yellow")
                .addComponent( TemplateHandler.Text.build()
                        .setText("这是一个策划书你还能肌肤恢复函数的积分和史蒂芬受到激发函数都返回")));
        // 第二个表格数据
        TableCell tableCell = TemplateHandler.Table.Cell.build();
        tableCell.setFontFamily("宋体").setFontSize("10").setWidth("145px")
                .setBorder("solid")
                //.setBorderColor("red")
        ;
        tableCell.addComponent(TemplateHandler.Text.build().setText("□是□否□其他"));
        row.addCell(tableCell);
        tableBody.addRow(row);
        table.setBody(tableBody);
        page.addBodyComponent(table);
        Table table1 = TemplateHandler.Table.build();
        TableBody tableBody1 = TemplateHandler.Table.Body.build();
        tableBody1.addRow(TemplateHandler.Table.Row.build().addCell()

                );
        table1.setBody(tableBody1);
        page.addBodyComponent(table1);
        create(tableBody);

        document.addPage(page);
        document.transform("D:\\pdf\\test\\fo\\" + ObjectId.next() + ".pdf");
    }

    private void create(TableBody tableBody) {


        TableRow row = TemplateHandler.Table.Row.build()
                .setBorder("solid").setMinRowHeight("20").setHorizontalStyle("center").setBorderStyle("yellow")
                .enableKeepTogether();
        //  第一个表格数据

        row.addCell(TemplateHandler.Table.Cell.build().setFontSize("10").setWidth("345px")
                .setFontFamily("宋体").setHorizontalStyle("left").setBorder("solid")//.setBorderColor("yellow")
                .addComponent( TemplateHandler.Text.build()
                        .setText("这是一个策划书你还能肌肤恢复函数的积分和史蒂芬受到激发函数都返回")));
        // 第二个表格数据
        TableCell tableCell = TemplateHandler.Table.Cell.build();
        tableCell.setFontFamily("宋体").setFontSize("10").setWidth("145px")
                .setBorder("solid")
        //.setBorderColor("red")
        ;
        tableCell.addComponent(TemplateHandler.Text.build().setText("□是□否□其他"));
        row.addCell(tableCell);
        tableBody.addRow(row);
    }


    /**
     * 设置页眉
     *
     * @param page
     */
    private void initHeader(Page page) {
        Table table = TemplateHandler.Table.build();
        table.setWidth("500pt")
                .setMarginLeft("50pt");
        TableBody tableBody = TemplateHandler.Table.Body.build();

        TableRow tableRow = TemplateHandler.Table.Row.build();
        tableRow.setMinRowHeight("20pt");
        TableCell tableCell = TemplateHandler.Table.Cell.build();
        tableCell.setWidth("250pt");
        Image image = TemplateHandler.Image.build()
                // 设置图像路径（绝对路径）
                .setPath("/D:\\pdf\\test\\fo\\jianfan.png")
                // 设置图像宽度
                .setWidth("50pt")
                // 设置图像高度
                .setHeight("17pt")
                .setHorizontalStyle("left");
        Text text = TemplateHandler.Text.build()
                .setText("江苏健帆信息研发").setFontSize("13pt").setFontColor("#0A72B7");
        BlockContainer blockContainer = TemplateHandler.BlockContainer.build();
        blockContainer.addComponent(image, text);
        tableCell.addComponent(blockContainer);
        tableRow.addCell(tableCell);


        Text text2 = TemplateHandler.Text.build()
                .setText("常规项信息测试").setFontSize("12pt");
        TableCell textCell = TemplateHandler.Table.Cell.build();
        textCell.setWidth("250pt").setVerticalStyle("after");
        textCell.setHorizontalStyle("right").addComponent(text2);
        tableRow.addCell(textCell);

        tableBody.addRow(tableRow);
        table.setBody(tableBody);
        SplitLine ridge = TemplateHandler.SplitLine.build()
                .setPaddingTop("-10pt").setStyle("ridge")
                .setLength("500pt").setMarginLeft("50pt");

        page.setHeaderHeight("50pt").addHeaderComponent(table, ridge);

    }


    /**
     * 设置页脚
     *
     * @param page
     */
    private void initFooter(Page page) {

        SplitLine ridge = TemplateHandler.SplitLine.build()
                .setStyle("ridge")
                .setLength("500pt").setMarginLeft("50pt");
        Table table = TemplateHandler.Table.build();
        TableBody tableBody = TemplateHandler.Table.Body.build();
        TableRow row1 = TemplateHandler.Table.Row.build();
        // 文件
        row1.setMinRowHeight("10pt").addCell(TemplateHandler.Table.Cell.build()
                .addComponent(TemplateHandler.Text.build().setText("内部文件,未经允许禁止泄露"))
                .setHorizontalStyle("center").setFontSize("10pt"));
        TableRow row2 = TemplateHandler.Table.Row.build();
        row2.setMinRowHeight("10pt").addCell(TemplateHandler.Table.Cell.build()
                .setFontSize("10pt").setHorizontalStyle("left")
                .addComponent(TemplateHandler.Text.build()
                        .setText("生效日期：2024-09-25")));
        TableRow row3 = TemplateHandler.Table.Row.build();
        row3.setMinRowHeight("10pt").addCell(TemplateHandler.Table.Cell.build()
                .setFontSize("10pt").setHorizontalStyle("left")
                .addComponent(TemplateHandler.Text.build()
                        .setText("文件编号：00001")));
        TableRow row4 = TemplateHandler.Table.Row.build();
        row4.setMinRowHeight("10pt").addCell(TemplateHandler.Table.Cell.build()
                .setFontSize("10pt").setHorizontalStyle("right")
                .addComponent(TemplateHandler.Text.build()
                        .setText("页码：1 / 8")));
        tableBody.addRow(row1, row2, row3, row4);
        table.setBody(tableBody);
        table.setWidth("500pt").setMarginLeft("50pt");
        page.setBodyMarginBottom("50pt").setFooterHeight("60pt").addFooterComponent(ridge, table);


    }


    /**
     * 测试表格底部显示边框
     */
    @Test
    public void test1() {
        Document document = TemplateHandler.Document.build();
        // 创建页面
        Page page = TemplateHandler.Page.build();
        page.setMarginTop("25pt").setFontFamily("宋体")
                .setMarginBottom("10pt").setWidth("600pt");
        page.setBodyBackgroundColor("#CCE8CF");
        Table table = TemplateHandler.Table.build();
        table.setWidth("500pt")
                .setMarginLeft("50pt");
        TableBody tableBody = TemplateHandler.Table.Body.build();

        TableRow tableRow = TemplateHandler.Table.Row.build();
        tableRow.setMinRowHeight("20pt");
        TableCell tableCell = TemplateHandler.Table.Cell.build();
        tableCell.setWidth("250pt");
        Image image = TemplateHandler.Image.build()
                // 设置图像路径（绝对路径）
                .setPath("/D:\\pdf\\test\\fo\\jianfan.png")
                // 设置图像宽度
                .setWidth("50pt")
                // 设置图像高度
                .setHeight("17pt")
                .setHorizontalStyle("left");
        Text text = TemplateHandler.Text.build()
                .setText("江苏健帆信息研发").setFontSize("13pt").setFontColor("#0A72B7");
        BlockContainer blockContainer = TemplateHandler.BlockContainer.build();
        blockContainer.addComponent(image, text);
        tableCell.addComponent(blockContainer);
        tableRow.addCell(tableCell);


        Text text2 = TemplateHandler.Text.build()
                .setText("常规项访视信息测试").setFontSize("12pt");
        TableCell textCell = TemplateHandler.Table.Cell.build();
        textCell.setWidth("250pt").setVerticalStyle("after");
        textCell.setHorizontalStyle("right").addComponent(text2);
        tableRow.addCell(textCell);

        tableBody.addRow(tableRow);
        table.setBody(tableBody);
        SplitLine ridge = TemplateHandler.SplitLine.build()
                .setPaddingTop("-10pt").setStyle("ridge")
                .setLength("500pt").setMarginLeft("50pt");

        page.setHeaderHeight("50pt").addHeaderComponent(table, ridge);


        // 创建表格
        Table tableData = TemplateHandler.Table.build();
        this.tableData(tableData);
        page.setBodyMarginTop("30pt").addBodyComponent(tableData);

        document.addPage(page);
        document.transform("D:\\pdf\\test\\fo\\" + ObjectId.next() + ".pdf");


    }


    private void tableData(Table table) {

// 创建表格体
        TableBody tableBody = TemplateHandler.Table.Body.build();
// 循环创建表格行
        for (int i = 1; i <= 20; i++) {
            // 创建表格行
            TableRow row = TemplateHandler.Table.Row.build();
            // 循环创建表格单元格
            for (int j = 1; j <= 2; j++) {
                // 创建表格单元格
                TableCell cell = TemplateHandler.Table.Cell.build();
                // 设置单元格边框
                cell.setBorder("1 solid black");
                // 创建文本
                Text text = TemplateHandler.Text.build().setText(String.format("%s-hello-world-%s", i, j));
                // 添加文本
                cell.addComponent(text);
                // 添加单元格
                row.addCell(cell);
            }
            // 添加行
            tableBody.addRow(row);
        }
// 设置表格体
        table.setBody(tableBody);
    }


}